package tic;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ticTactoe extends JFrame{
    private static final int NUMBER = 3;

    private static final int PERSON = 1;
    private static final int AI = -1;
    private static final int SEARCHDEPTH = 9;
    private static final int INT_MAX = 999999;
    private static final int INT_MIN = -1000000;
    private static int player = 0;
    private static boolean isGameOver = false;
    private static int currentDepth = 0;
    private static int[] bestPosition = new int[2];
    private static final JLabel messageLbel = new JLabel("该你了，小可爱          ");
    private static final JButton resetButton = new JButton("不服，再来一次");
    private static  JLabel curDep = new JLabel();
    private static final JButton[] boardButton = new JButton[9];
    private static final JPanel tag = new JPanel();
    private static  int[][] chessBoard = new int[NUMBER][NUMBER];
    public static void setChessBoard(int[][] chessBoard) {
        ticTactoe.chessBoard = chessBoard;
    }

    //继承了JFrame类
    Container container=this.getContentPane();
    public ticTactoe() {;
        //修改图标
        setIconImage(Toolkit.getDefaultToolkit().getImage("./ic_launcher2.png"));
        //窗口的长宽
        this.setSize(500, 500);
        //窗口的坐标
        this.setLocation(200, 200);
        //窗口位置居中
        this.setLocationRelativeTo(null);
        //自定义窗口标题
        this.setTitle("TicTacToe——zxzchh");
        //设置窗口不可更改大小
        this.setResizable(false);
        //默认不显示窗口,此语句显示窗口，且必须位于最后一行，相当于打印窗口
        this.setVisible(true);

        //createBack()面板添加到页面中
        this.setContentPane(createBack());
    }
    private static Container createBack() {
        JPanel back = new JPanel();//创建面板对象
        back.setLayout(new BorderLayout());//设置布局方式
        back.add(createBoard(),BorderLayout.CENTER);
        back.add(createTag(),BorderLayout.NORTH);//北部，顶部位置
        return back;
    }
    private static Component createTag(){

        tag.setLayout(new FlowLayout());
        tag.add(messageLbel);//为了后面全局调用
        tag.add(resetButton);//为了全局调用
        tag.add(curDep);
        //添加监听事件
        resetButton.addActionListener(ticTactoe::reClicked);//类名
        return tag;
    }
    private static void reClicked(ActionEvent actionEvent) {
        int [][] board = {{0,0,0},{0,0,0},{0,0,0}};
        setChessBoard(board);
        for (int i = 0;i<9;i++){
            boardButton[i].setText("");
        }
        player = PERSON;
        isGameOver = false;
        currentDepth = 0;
        messageLbel.setText("到你了！");
        curDep.setText("当前深度"+currentDepth);
    }

    private static Component createBoard() {//Component组件
        JPanel board = new JPanel();
        board.setLayout(new GridLayout(3,3));
        for (int i = 0;i<9;i++){

                boardButton[i] = new JButton();
                //设置颜色
                boardButton[i].setBackground(Color.pink);
                //设置字体与型号
                boardButton[i].setFont(new Font("Ink Free", Font.PLAIN, 100));
                //添加监听事件
                boardButton[i].addActionListener(ticTactoe::userClicked);//类名
                board.add(boardButton[i]);

        }
        return board;
    }

    private static void userClicked(ActionEvent actionEvent) {
        JButton button = (JButton)  actionEvent.getSource();
        button.setText("X");
        curDep.setText("当前深度"+currentDepth);
        System.out.println(button.getX());
        Logic();
        personPlay(button.getY()/142,button.getX()/162);
        if (isEnd() == 0&&currentDepth<8)
        {
           aiPlay();
            if (isEnd() != 0)
            {
                isGameOver = true;
            }
        }
        else
        {
            isGameOver = true;
        }
    }
    private static void personPlay(int x,int y){
        chessBoard[x][y] = PERSON;

        currentDepth++;

        player = AI;
    }
    private static void aiPlay(){
        MinMaxSearch(currentDepth);
        chessBoard[bestPosition[0]][bestPosition[1]]=AI;
        if (bestPosition[0]==0&&bestPosition[1]==0){boardButton[0].setText("O");}
        if (bestPosition[0]==0&&bestPosition[1]==1){boardButton[1].setText("O");}
        if (bestPosition[0]==0&&bestPosition[1]==2){boardButton[2].setText("O");}
        if (bestPosition[0]==1&&bestPosition[1]==0){boardButton[3].setText("O");}
        if (bestPosition[0]==1&&bestPosition[1]==1){boardButton[4].setText("O");}
        if (bestPosition[0]==1&&bestPosition[1]==2){boardButton[5].setText("O");}
        if (bestPosition[0]==2&&bestPosition[1]==0){boardButton[6].setText("O");}
        if (bestPosition[0]==2&&bestPosition[1]==1){boardButton[7].setText("O");}
        if (bestPosition[0]==2&&bestPosition[1]==2){boardButton[8].setText("O");}
        curDep.setText("当前深度"+currentDepth);
        currentDepth++;
        player = PERSON;
    }
    private static int  MinMaxSearch(int depth){
        int value = 0;
        if (player == PERSON){ value = INT_MIN;}
        if (player == AI){ value = INT_MAX;}
        if (isEnd()!=0){
            return Evaluate();
        }
        if (depth == SEARCHDEPTH){
            value = Evaluate();
            return value;
        }
        for (int i = 0;i<NUMBER;i++){
            for (int j =0;j<NUMBER;j++){
                if (chessBoard[i][j]==0){
                    if (player == PERSON){
                        chessBoard[i][j]=PERSON;
                        player = AI;
                        int nextvalue =MinMaxSearch(depth+1);
                        player =PERSON;
                        if (value<nextvalue){
                            value = nextvalue;
                            if (depth==currentDepth){
                                bestPosition[0]=i;
                                bestPosition[1]=j;
                            }
                        }
                    }
                    else if(player ==AI){
                        chessBoard[i][j]=AI;
                        player = PERSON;
                        int nextvalue = MinMaxSearch(depth+1);
                        player =AI;
                        if (value>nextvalue){
                            value = nextvalue;
                            if (depth==currentDepth){
                                bestPosition[0]=i;
                                bestPosition[1]=j;
                            }
                    }
                }
                    chessBoard[i][j]=0;
                }
            }
        }
        return value;
    }
    private static void Logic(){
        if(isGameOver){
            if (isEnd() == PERSON) {
                messageLbel.setText("你赢了！");
            } else if (isEnd() == AI) {
                messageLbel.setText("你输了！");
            } else {
                messageLbel.setText("平局！");
            }
        }
    }
    private static int isEnd(){
        //短路或||不管第二项和后面是什么，都不判断
        //非短路或|逐一判断
        int i;
        int j;
        int count = 0;
        for (i=0;i<NUMBER;i++){
            count=0;
            for (j=0;j<NUMBER;j++){
                count += chessBoard[i][j];
            }
            if (count == 3 || count == -3){
                return (count/3);
            }
        }
        for (j=0;j<NUMBER;j++){
            count=0;
            for (i=0;i<NUMBER;i++){
                count += chessBoard[i][j];
            }
            if (count == 3 || count == -3){
                return (count/3);
            }
        }
        count = 0;
        count = chessBoard[0][0]+chessBoard[1][1]+chessBoard[2][2];
        if(count == 3 ||count == -3){
            return (count/3);
        }
        count = chessBoard[0][2]+chessBoard[1][1]+chessBoard[2][0];
        if (count == 3||count == -3){
            return (count/3);
        }
        return 0;
    }
    private static int Evaluate(){
        int value = isEnd();
        if (value == PERSON){return INT_MAX;}
        if (value == AI){return INT_MIN;}
        return value;
        }
    //主程序入口
    public static void main(String[] args) {
        new ticTactoe();
    }
}